import execjs, requests


def down(url, uid, ctx):
    headers = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Length": "310",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Cookie": "__jsluid_s=b08fa4bef2818d7895464a9194af15aa; global_cookie=8rk9su4tjtvu2z2br66j9m09q22l7a7rqzt; g_sourcepage=txz_dl%5Egg_pc; __utmc=147393320; __utmz=147393320.1661502639.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=147393320.813354768.1661502639.1661502639.1661570321.2; token=331d7d30123645e1ba7aa06d37c970ee; unique_cookie=U_8rk9su4tjtvu2z2br66j9m09q22l7a7rqzt*3",
        "Host": "passport.fang.com",
        "Origin": "https://passport.fang.com",
        "Pragma": "no-cache",
        "Referer": "https://passport.fang.com/",
        "sec-ch-ua": "\"Chromium\";v=\"21\", \" Not;A Brand\";v=\"99\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest"
    }
    data = {
        "uid": uid,
        "pwd": ctx,
        "Service": "soufun-passport-web",
        "AutoLogin": "1"
    }
    res = requests.post(url, data=data,headers=headers)
    return res


def js_code(pwd):
    with open('./js/房天下.js', 'r', encoding='utf-8') as f:
        js_cont = f.read()
    ctx = execjs.compile(js_cont).call('xx', pwd)

    print(ctx)
    return ctx


if __name__ == '__main__':
    url = 'https://passport.fang.com/login.api'
    pwd = "123456"
    ctx = js_code(pwd)
    res = down(url, uid=12345, ctx=ctx)
    print(res.json())
